package clients

import (
	"context"
	"strconv"

	"gitee.com/shiwjlinux/arm/apps/instance"
)

var _ instance.Service = (*Client)(nil)

func (c *Client) CreateInstance(ctx context.Context,
	in *instance.Instance) (*instance.Instance, error) {
	// c.logger.Debug().Msgf("AAAAAAAAAAAAAAAAA %+v", in)
	ins := instance.NewInstance()
	resp, err := c.C.R().
		SetContext(ctx).
		SetBody(in).
		SetResult(ins).
		Post("/arm/api/v1/instance/")
	if err != nil {
		return nil, err
	}

	return resp.Result().(*instance.Instance), nil
}

func (c *Client) DeleteInstance(context.Context,
	*instance.DeleteInstanceRequest) (*instance.Instance, error) {
	return nil, nil
}

func (c *Client) DescribeInstance(context.Context,
	*instance.DescribeInstanceRequest) (*instance.Instance, error) {
	return nil, nil
}

func (c *Client) QueryInstance(ctx context.Context,
	in *instance.QueryInstanceRequest) (*instance.InstanceSet, error) {
	set := instance.NewInstanceSet()
	_, err := c.C.R().
		SetContext(ctx).
		SetResult(set).
		SetQueryParams(map[string]string{
			"ID":          in.ID,
			"sn":          in.Sn,
			"keywords":    in.Keywords,
			"page_number": strconv.Itoa(in.PageNumber),
			"page_size":   strconv.Itoa(in.PageSize),
			// TODO: Tags
		}).
		Get("/arm/api/v1/instance/")
	if err != nil {
		return nil, err
	}

	// fmt.Printf("%+v\n", resp)
	return set, nil
}

func (c *Client) UpdateHeartbeat(ctx context.Context,
	in *instance.UpdateHeartbeatRequest) (*instance.Instance, error) {
	// i.logger.Debug().Msgf(" (i *Client) UpdateHeartbeat in %v", in)
	return nil, nil
}

func (c *Client) UpdateHeartbeats(ctx context.Context,
	in *instance.UpdateHeartbeatRequests) error {
	// c.logger.Debug().Msgf(" (i *Client) UpdateHeartbeat in %v", in)
	_, err := c.C.R().
		SetContext(ctx).
		SetBody(in).
		Post("/arm/api/v1/instance/heartbeats")
	if err != nil {
		return err
	}
	return nil
}
